//
//  AboutUsViewController.m
//  PrintPort
//
//  Created by 华谕 on 2017/11/17.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "AboutUsViewController.h"
#import "ZGLVideoPlyer.h"

static NSString *cellPath = @"aboutcell";
@interface AboutUsViewController ()<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>

@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong) ZGLVideoPlyer *player;
@property (nonatomic, strong)NSString *videoSd;
@property (nonatomic, strong)NSString *urlStr;
@property (nonatomic, strong)UIWebView *webView;

@end

@implementation AboutUsViewController

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.player pause];
    
    
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    NSInteger num = self.navigationController.viewControllers.count;
    if (num == 0 && _player)
    {
        [_player removeALLnotilotion];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _urlStr = @"";
    [self creatUI];
    [self requestagreement];
}

- (void)creatUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"关于我们";
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    CGFloat HeightX = 0;
    if (SCREEN_HEIGHT == 812)
    {
        HeightX = 34*displayheight;
    }
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-HeightX) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.backgroundColor = [UIColor whiteColor];
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
//    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 200*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT+10*displayheight)];
//    [self.view addSubview:webView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0)
    {
        if (_videoSd.length > 0)
        {
            if (!self.player)
            {
                NSLog(@"1111111111");
                [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                self.player = [[ZGLVideoPlyer alloc]initWithFrame:CGRectMake(15*displaywidth, 10*displaywidth, SCREEN_WIDTH-30*displaywidth, 180*displayheight)];
                [_player playerBindTableView:self.tableview currentIndexPath:indexPath];
                self.player.videoUrlStr = _videoSd;
                [self.player pause];
            }
            [cell.contentView addSubview:self.player];

            UIView *linevcie = [[UIView alloc] initWithFrame:CGRectMake(0, 199*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
            linevcie.backgroundColor = LinesColor;
            [cell.contentView addSubview:linevcie];
        }
    }
    else
    {
        if (![_urlStr isEqualToString:@""])
        {
            if (!self.webView)
            {
                [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                _webView = [[UIWebView alloc]initWithFrame:CGRectMake(15*displaywidth, 20*displaywidth, SCREEN_WIDTH-30*displaywidth, 200*displaywidth)];
                _webView.delegate = self;
                _webView.scrollView.scrollEnabled = NO;
                //预先加载url
                [self.webView loadHTMLString:_urlStr baseURL:nil];
            }
            [cell.contentView addSubview:_webView];

        }
       
        
    }
    
    return cell;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //获取到webview的高度
    CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    self.webView.frame = CGRectMake(self.webView.frame.origin.x,self.webView.frame.origin.y, SCREEN_WIDTH, height+40*displaywidth);
    [self.tableview reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 200*displaywidth;
    }
    return _webView.frame.size.height+40*displaywidth;
    
}

- (void)requestagreement
{
    WEAKSELF;
    NSDictionary *dic = @{@"type":@"1"};
    [HYBaseHandler requestagreementParmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            NSArray *array = obj[@"data"];
            if (array.count > 0)
            {
                NSDictionary *dictionary = array[0];
                _urlStr = dictionary[@"text"];
                _videoSd = dictionary[@"videoUrl"];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf.tableview reloadData];
                });
            }
            
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
